<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>事件高级-4(阻止事件冒泡)</title>
    <style>
        .father {
            position: relative;
            width: 300px;
            height: 300px;
            margin: 100px auto;
            background-color: aquamarine;
        }
        
        .father .son {
            position: absolute;
            width: 200px;
            height: 200px;
            margin: auto;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            text-align: center;
            font-weight: 600px;
            color: #fff;
            line-height: 200px;
            background-color: orange;
        }
    </style>
</head>

<body>
    <div class="father">
        <div class="son">子盒子</div>
    </div>
    <script>
        // 1.阻止事件冒泡的两种方式
        // 事件冒泡:开始时由最具体的元素接收,然后逐层向上传播到DOM最顶层节点
        // 冒泡阶段:son->father->body->html->document  第三个参数如果是flase or 省略不写

        // 阻止冒泡 DOM推荐的标准 e.stopPropagation()方法
        var son = document.querySelector('.son');
        son.addEventListener('click', function(e) {
            alert('The current result of this program is SON ');

            e.stopPropagation(); // stop 停止 propagation 传播

            // 低版本浏览器 ie9 以下 e.cancelBubble 属性
            e.cancelBubble = true; // cancel 取消 bubble 气泡 v.冒泡 

        })

        // 阻止事件冒泡的兼容性解决方案
        // if (e && e.shopPropagetion) {
        //     e.shopPropagetion();
        // } else {
        //     window.event.cancelBubble = true;
        // }


        var father = document.querySelector('.father');
        father.addEventListener('click', function() {
            alert('The current result of this program is Father ');
        }, false)

        document.addEventListener('click', function() {
            alert('The current result of this program is Docunment');
        }, false)
    </script>
</body>

</html>